package com.example.stmall.manager.controller.api;

import com.example.stmall.manager.entity.Result;
import com.example.stmall.manager.util.UploadUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


/**
 * 文件上传的Controller
 */
@RestController
@RequestMapping("/api/upload")
public class APIUploadController {

    @Value("${upload.path}")
    //使用Spring的EL表达式，读取配置文件中，upload.path属性的值
    private String path;

    /**
     * 上传文件的操作应该是用POST
     */
    @PostMapping
    public Result upload(MultipartFile file){
        String fileName = UploadUtil.save(file,path);
        if(fileName != null) {
            return Result.success(fileName);
        }else{
            return Result.error("上传失败！");
        }
    }
}
